package com.handytoo.cow.client;

import com.handytoo.cow_core.network.Coder.LengthDecoder;
import com.handytoo.cow_core.network.Coder.LengthEncoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;


/**
 * 调试用客户端
 */
public class NettyClient {
    public static void main(String[] args) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline pipeline = ch.pipeline();

                            // 编解码器配置（与服务器对称）
                            pipeline.addLast("encoder", new LengthEncoder());
                            pipeline.addLast("decoder", new LengthDecoder());
                            // 业务处理器
                            pipeline.addLast(new ClientHandler());
                        }
                    });

            ChannelFuture f = b.connect("localhost", 51112).sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}
